
import java.io.IOException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Javier
 */
public class MyCanvas extends Canvas implements Runnable{
    public static int wCanvas = 0;
    public static int hCanvas = 0;
    private boolean running = true;

    public static Image imgBg;

    private static final int MAX_CPS = 50;
    private static final int MS_PER_FRAME = 1000 / MAX_CPS;

    private Game game;
    private MainMenu mainMenu;

    public static long cycleStartTime;
    public static boolean moveUp;
    public static boolean moveDown;
    public static boolean moveLeft;
    public static boolean moveRight;
    public static boolean fire;
    public static boolean isClockwise=false;
    public static boolean isFinished= false;

    public static final int MENU_OPTION=0;
    public static final int GAME_OPTION=1;
    public static int classOption;

    public MyCanvas(){
        //get the screen size
        wCanvas = this.getWidth();
        hCanvas = this.getHeight();

        //load the background image
        try {
            imgBg = Image.createImage("/bg.png");
        } catch (IOException ex)
        { }

    }

    protected void paint(Graphics g) {
        //render the enabled part
        if(game != null){
            game.paint(g);
        }
        else{
            mainMenu.paint(g);
        }
    }

    public void run() {
        while(running){
            // remember the starting time
            cycleStartTime = System.currentTimeMillis();

            //change which part is enabled if it's necessary
            if((classOption==MENU_OPTION) && (mainMenu==null)){
                game = null;
                mainMenu = new MainMenu();
            }
            else if((classOption==GAME_OPTION) && (game==null)){
                game = new Game(Game.SURVIVAL_MODE);
                mainMenu = null;
            }

            processTick();

            repaint();
            serviceRepaints();

            // sleep if we've finished our work early
          long timeSinceStart = (cycleStartTime - System.currentTimeMillis());
          if (timeSinceStart < MS_PER_FRAME)
          {
             try
             {
                Thread.sleep(MS_PER_FRAME - timeSinceStart);
             }
             catch(java.lang.InterruptedException e)
             { }
          }
        }

    }

    private void processTick() {
        if(game != null){
            game.processTick();
        }
        else{
            mainMenu.processTick();
        }
    }

    protected void keyPressed(int keyCode) {
      int key = getGameAction(keyCode);
      //store in the booleans which keys are pressed
      switch (key){
          case Canvas.UP:
              moveUp = true;
              break;
          case Canvas.DOWN:
              moveDown = true;
          break;

          case Canvas.LEFT:
              moveLeft = true;
          break;

          case Canvas.RIGHT:
            moveRight = true;
          break;

          case Canvas.FIRE:
              fire = true;
              break;

      }
    }

    protected void keyReleased(int keyCode) {
      int key = getGameAction(keyCode);
      //store in the booleans which keys are released
      switch (key){
          case Canvas.UP:
              moveUp = false;
          break;
          case Canvas.DOWN:
              moveDown = false;
          break;

          case Canvas.LEFT:
              moveLeft = false;
          break;

          case Canvas.RIGHT:
            moveRight = false;
          break;
          
          case Canvas.FIRE:
              fire = false;
              //if the fire button is released change the player's spin
              isClockwise=!isClockwise;            
          break;

      }
    }


}
